using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.SceneManagement;
using UnityEngine.ResourceManagement.ResourceProviders;

public class LoadSingle : MonoBehaviour
{
    public string host = "http://localhost";
    public string path = "Lightmap";
    public string catalog = "catalog_Ligtmap.json";

    public string keyOfScene1 = "Assets/Scenes/Scene1.unity";
    public string keyOfScene2 = "Assets/Scenes/Scene2.unity";
    public string keyOfSceneBoth = "Assets/Scenes/SceneBoth.unity";

    public string nameOfScene1 = "Scene1";
    public string nameOfScene2 = "Scene2";
    public string nameOfSceneBoth = "SceneBoth";

    public LoadSceneMode loadMode = LoadSceneMode.Additive;

    private bool catalogLoaded = false;
    private int loadScene = 0;//1=scene1, 2=scene2, 12=sceneboth
    void LoadCatalog()
    {
        string fullCatalog = string.Format("{0}/{1}/{2}", host, path, catalog);
        
        Debug.Log(fullCatalog);
        Debug.Log("准备加载Catalog...");

        Addressables.LoadContentCatalogAsync(fullCatalog).Completed += LoadContentCatalogCompleted;
    }

    private void LoadContentCatalogCompleted(AsyncOperationHandle<IResourceLocator> obj)
    {
        if (obj.Status == AsyncOperationStatus.Succeeded)
        {
            Debug.LogFormat("Catalog加载成功 LocatorId={0}", obj.Result.LocatorId);
            catalogLoaded = true;
            if (loadScene == 1)
            {
                DoLoadScene(keyOfScene1);
            }
            else if (loadScene == 2)
            {
                DoLoadScene(keyOfScene2);
            }
            if (loadScene == 12)
            {
                DoLoadScene(keyOfSceneBoth);
            }
        }
        else
        {
            Debug.LogError("Catalog加载失败");
        }
    }

    public void UnloadFirst()
    {
        if (hasPrevScene)
        {
            hasPrevScene = false;
            Addressables.UnloadSceneAsync(prevSceneHandle).Completed += UnLoadSceneCompleted;

            //Addressables.ReleaseInstance(prevSceneHandle);
        }
    }

    private void UnLoadSceneCompleted(AsyncOperationHandle<SceneInstance> obj)
    {

    }

    private bool hasPrevScene = false;
    private AsyncOperationHandle<SceneInstance> prevSceneHandle;
    private void DoLoadScene(string keyOfScene)
    {
        Addressables.LoadSceneAsync(keyOfScene, loadMode).Completed += LoadScene_Completed;
    }

    private void LoadScene_Completed(AsyncOperationHandle<SceneInstance> obj)
    {
        if (obj.Status == AsyncOperationStatus.Succeeded)
        {
            hasPrevScene = true;
            prevSceneHandle = obj;

            Debug.Log("Scene加载成功");
            //SceneManager.SetActiveScene(obj.Result.Scene);
            obj.Result.ActivateAsync();
        }
        else
        {
            Debug.LogError("Scene加载失败");
        }
    }

    //private void LoadSingle_Completed(AsyncOperationHandle<Scene> obj)
    //{
    //    if (obj.Status == AsyncOperationStatus.Succeeded)
    //    {
    //        string sceneName = "";

    //        if (loadScene == 1)
    //        {
    //            sceneName = nameOfScene1;
    //        }
    //        else if (loadScene == 2)
    //        {
    //            sceneName = nameOfScene2;
    //        }
    //        if (loadScene == 12)
    //        {
    //            sceneName = nameOfSceneBoth;
    //        }

    //        SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
    //    }
    //}

    public void LoadScene1()
    {
        if (!catalogLoaded)
        {
            loadScene = 1;
            LoadCatalog();
            return;
        }

        DoLoadScene(keyOfScene1);
    }

    public void LoadScene2()
    {
        if (!catalogLoaded)
        {
            loadScene = 2;
            LoadCatalog();
            return;
        }

        DoLoadScene(keyOfScene2);
    }

    public void LoadSceneBoth()
    {
        if (!catalogLoaded)
        {
            loadScene = 12;
            LoadCatalog();
            return;
        }

        DoLoadScene(keyOfSceneBoth);
    }
}
